---@class List_Item : LuaObject @队列ITEM
local List_Item = BaseClass("List_Item")

function List_Item:__ctor()
	self:clear()
end

function List_Item:clear()
	self.mPre = nil      -- 前置指针
	self.mNext = nil     -- 后置指针
	self.mValue = nil    -- 数值
end

-- 可用于各种需要支持LIST操作的地方
---@class List : LuaObject @LIST通用类
local this = BaseClass("List")

function this:__ctor()
	self.mCount = 0        -- 元素个数
	self.mFirst = nil      -- 首位元素
end

-- 得到元素个数
function this:Count()
	return self.mCount
end

-- 得到第一个有效元素的节点指针
function this:Begin()
	return self.mFirst
end

-- 得到指定位置的元素
function this:Get(pos)
	local temp = nil
	if(self.mCount <= 0)then
		return temp
	end

	if (pos <= 0 or pos > self.mCount) then
		return temp
	end

	-- 挨个遍历
	for i = 1, pos do
		if (temp == nil) then
			temp = self.mFirst
		else
			temp = temp.mNext
		end
	end
	return temp
end

-- 插入
function this:Push(value)
	local temp = List_Item()
	temp.mValue = value
	self.mCount = self.mCount + 1

	if (self.mFirst == nil) then
		self.mFirst = temp
		return
	end

	local endNode = self.mFirst
	while (endNode.mNext ~= nil) do
		endNode = endNode.mNext
	end

	endNode.mNext = temp
	temp.mPre = endNode
end

-- 删除第一个节点
function this:Pop()
	if (self.mCount <= 0) then return end

	local temp = self:Erase(1)
	if (temp ~= nil) then
		temp:clear()
	end
end

-- 删除
function this:Erase(pos)
	if (self.mFirst == nil) then return end

	if (pos == 1) then
		local temp = self.mFirst
		self.mFirst = self.mFirst.mNext

		if (self.mFirst ~= nil) then
			self.mFirst.mPre = nil
		end

		self.mCount = self.mCount - 1
		return temp
	end

	local temp = self:Get(pos)
	if (temp == nil) then
		return temp
	end

	if (temp.mPre ~= nil) then
		temp.mPre.mNext = temp.mNext
	end
	if (temp.mNext ~= nil) then
		temp.mNext.mPre = temp.mPre
	end

	self.mCount = self.mCount - 1
	return temp
end

-- 删除
function this:Del(temp)
	if (self.mFirst == nil) then return end

	if (self.mFirst == temp) then
		temp = self.mFirst
		self.mFirst = self.mFirst.mNext

		if (self.mFirst ~= nil) then
			self.mFirst.mPre = nil
		end

		self.mCount = self.mCount - 1
		return temp
	end

	if (temp.mPre ~= nil) then
		temp.mPre.mNext = temp.mNext
	end
	if (temp.mNext ~= nil) then
		temp.mNext.mPre = temp.mPre
	end

	self.mCount = self.mCount - 1
	return temp
end

-- 全部清除
function this:Clear(datacallback)
	while (self.mFirst ~= nil) do
		local temp = self.mFirst
		self.mFirst = temp.mNext

		if (datacallback) then
			datacallback(temp.mData)
		end
		temp:clear()
	end

	self.mFirst = nil
	self.mCount = 0
end

List = this